package com.wxg.junit.appendix.B3;

import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;

public class IsStrongPassword extends TypeSafeMatcher<String> {

    @Override
    public void describeTo(Description description) {
        description.appendText("string that contains"
                + " a digit, a special character and is at least 6 symbols");
    }

    @Override
    protected boolean matchesSafely(String item) {
        return containsSpecialSymbo(item)
                && containsDigit(item)
                && item.length() >= 6;
    }

    private boolean containsDigit(String item) {
        for (char ch : item.toCharArray()) {
            if (Character.isDigit(ch)) {
                return true;
            }
        }
        return false;
    }

    private boolean containsSpecialSymbo(String item) {
        for (char ch : item.toCharArray()) {
            int chi = (int) ch;
            if (chi <= 47 && chi >= 33) {
                return true;
            }
        }
        return false;
    }

    @Factory
    public static <T> Matcher<String> isStrongPassword() {
        return new IsStrongPassword();
    }

    @Factory
    public static <T> Matcher<String> strongPassword() {
        return new IsStrongPassword();
    }

}
